<%@ page language="java" contentType="text/html; charset=utf-8"
         pageEncoding="utf-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>门店管理-新增部门</title>
    <meta name="keywords" content="H+后台主题,后台bootstrap框架,会员中心主题,后台HTML,响应式后台">
    <meta name="description" content="H+是一个完全响应式，基于Bootstrap3最新版本开发的扁平化主题，她采用了主流的左右两栏式布局，使用了Html5+CSS3等现代技术">
    <link rel="shortcut icon" href="favicon.ico">
    <link href="<%=basePath%>hplus/css/bootstrap.min14ed.css?v=3.3.6" rel="stylesheet">
    <link href="<%=basePath%>hplus/css/font-awesome.min93e3.css?v=4.4.0" rel="stylesheet">
    <link href="<%=basePath%>hplus/css/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="<%=basePath%>hplus/css/animate.min.css" rel="stylesheet">
    <link href="<%=basePath%>hplus/css/style.min862f.css?v=4.1.0" rel="stylesheet">
    <style>
        .col-sm-1 {
            padding-right: 0;
        }

        input.uploadFile {
            position: absolute;
            opacity: 0;
            width: 100%;
            height: 100%;
            left: 0;
            top: 0;
            line-height: 0;
            padding-left: 0;
            margin-left: 0;
        }

        .uploadBtn {
            width: 60px;
            display: inline-block;
            background: #1ab394;
            color: #fff;
            border-radius: 3px;
            height: 36px;
            line-height: 36px;
            border: none;
            font-size: 14px;
        }

        .s_address {
            height: 340px;
            background: #eee;
        }

        .red {
            color: red;
            margin-right: 5px;
        }
    </style>
</head>

<body class="gray-bg">
<div class="wrapper wrapper-content">
    <div class="ibox-content">
        <form method="post" class="form-horizontal" id='share_image' enctype='multipart/form-data'>
            <div class="form-group">
                <label class="col-sm-1 control-label"><span class="red">*</span>门店名称</label>
                <div class="col-sm-4">
                    <input type="text" id="name" class="form-control" maxlength="10" placeholder="请勿超过十个字">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-1 control-label"><span class="red">*</span>联系电话</label>
                <div class="col-sm-4">
                    <input type="text" id="phone" class="form-control"
                           onkeyup="this.value=this.value.replace(/\D/g,'')"
                           onafterpaste="this.value=this.value.replace(/\D/g,'')" maxlength="20" placeholder="请填写联系电话">
                </div>
            </div>
            <div class="form-group" id="target">
                <label class="col-sm-1 control-label"><span class="red">*</span>门店地区</label>
                <div class="col-sm-2">
                    <select class="form-control" id="s_province" name="s_province"></select>
                </div>
                <div class="col-sm-2">
                    <select class="form-control" id="s_city" name="s_city"></select>
                </div>
                <div class="col-sm-2">
                    <select class="form-control" id="s_county" name="s_county" onchange="selectAddress()"></select>
                </div>
                <div id="show"></div>
            </div>
            <div class="form-group">
                <label class="col-sm-1 control-label"><span class="red">*</span>详细地址</label>
                <div class="col-sm-6">
                    <input type="text" class="form-control" id="address" maxlength="30">
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-1 control-label" style="visibility: hidden;">详细地址</label>
                <div class="col-sm-11 s_address" id="allmap">
                </div>
            </div>

            <!-- 设置门店密码 -->
            <div class="form-group">
                <label class="col-sm-1 control-label">门店密码</label>
                <div class="col-sm-4">			
                    <input id="storePass" class="form-control" maxlength="6" type="password" placeholder="请设置6位数字密码">
                </div>
            </div>										
            <!-- 确认密码 -->
            <div class="form-group">
                <label class="col-sm-1 control-label">确认密码</label>
                <div class="col-sm-4">										
                    <input id="storePassCon" class="form-control" type="password" maxlength="6" placeholder="请再次输入密码">
                </div>
            </div>
            												
            <div class="form-group">
                <label class="col-sm-1 control-label">组织机构代码</label>
                <div class="col-sm-4">
                    <input type="text" class="form-control" id="organizationCode"
                           onkeyup="value=value.replace(/[^\w\.\/]/ig,'')">
                </div>
            </div>

            <!-- <div class="form-group">
                <label class="col-sm-1 control-label">组织机构代码证</label>
                <div class="col-sm-6">
                    <button class="uploadBtn" onclick="fileUpload()" type="button" style="margin-left: 10px">上传
                    </button>
                    <input class="uploadFile" type="file" id="inputfile"  name="filepath"
                               style="display:none;filter: alpha(opacity = 50); -moz-opacity: 0.5; -khtml-opacity: 0.5; opacity: 0.0; position: absolute; z-index: 99;">
                    <span class="help-block m-b-none" id="fileName">已选文件：代码证</span>
                    <div class="clear-shadow"></div>
                </div>
            </div> -->
            
            <div class="form-group">
                <div class="col-sm-2 col-sm-offset-1">
                    <button type="button" class="btn btn-outline btn-primary" id="close" style="margin-right:20px;"
                            organizationImg="">取消
                    </button>
                    <button type="button" class="btn btn-primary k_verify" id="commit">确定</button>
                </div>
            </div>
        </form>
    </div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/bootcdn/BootCDN/ajax/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="<%=basePath%>hplus/js/content.min.js?v=1.0.0"></script>
<script src="<%=basePath%>hplus/js/plugins/chosen/chosen.jquery.js"></script>
<script src="<%=basePath%>hplus/js/plugins/jsKnob/jquery.knob.js"></script>
<script src="<%=basePath%>hplus/js/plugins/jasny/jasny-bootstrap.min.js"></script>
<script src="<%=basePath%>hplus/js/plugins/datapicker/bootstrap-datepicker.js"></script>
<script src="<%=basePath%>hplus/js/plugins/prettyfile/bootstrap-prettyfile.js"></script>
<script src="<%=basePath%>hplus/js/plugins/nouslider/jquery.nouislider.min.js"></script>
<script src="<%=basePath%>hplus/js/plugins/switchery/switchery.js"></script>
<script src="<%=basePath%>hplus/js/plugins/ionRangeSlider/ion.rangeSlider.min.js"></script>
<script src="<%=basePath%>hplus/js/plugins/iCheck/icheck.min.js"></script>
<script src="<%=basePath%>hplus/js/plugins/metisMenu/jquery.metisMenu.js"></script>
<script src="<%=basePath%>hplus/js/plugins/colorpicker/bootstrap-colorpicker.min.js"></script>
<script src="<%=basePath%>hplus/js/plugins/clockpicker/clockpicker.js"></script>
<script src="<%=basePath%>hplus/js/plugins/cropper/cropper.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/artDialog/7.0.0/dialog-plus.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<script type="text/javascript" src="<%=path%>/js/marketing/public.js"></script>
<script type="text/javascript" src="<%=path%>/js/marketing/jquery-form.js"></script>
<script type="text/javascript" src="<%=path%>/hplus/js/demo/distpicker.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/artDialog/7.0.0/dialog-plus.js"></script>
<script type="text/javascript"
        src="http://api.map.baidu.com/api?v=2.0&ak=6lNan2DV0rYwYRwqOW6rDhLHMw0KddKK"></script>
<script type="text/javascript"  src="<%=path%>/js/system/province-city-area-data.js"></script>
<script type="application/javascript">

    var access_token = $.cookie('access_token');
    var cid = $.cookie('cid');
    var uid = $.cookie('uid');
    var organizationImg = "";
    var address = "";
    var name = "";
    var phone = "";
    var organizationCode = "";
    var province = "";
    var city = "";
    var area = "";
    var lng = "";
    var lat = "";
    var storePassCon = "";
    var flag = getQueryString("flag");
    var id = getQueryString("id");
    $(function () {

        if (StringEmpty(flag)) {
            if (StringEmpty(id)) {
                _init_map(flag);
                queryOne();
            }
            else {
                $('#target').distpicker({
                    province: "省份",
                    city: "地级市",
                    district: "市、县级市"
                });
                _init_map(null);
            }
        }
    })

    //根据id查询门店
    function queryOne() {
        var searchUrl = "<%=path%>/store/ajaxQueryStore";
        $.ajax({
            url: searchUrl,
            type: "post",
            async: false,
            data: {
                "access_token": access_token,
                "cid": cid,
                "uid": uid,
                "id": id,
            }, success: function (data) {
                var datas = data;
                if (datas.status == "200") {
                    $("#name").val(datas.data.name);
                    $("#phone").val(datas.data.phone);
                    $("#address").val(datas.data.address);
                    $("#organizationCode").val(datas.data.organizationCode);
                    if(datas.data.pwd != "" && datas.data.pwd != null){
                    $("#storePass").val("abcdef");
                    $("#storePassCon").val("abcdef");
                    }else{
                    $("#storePass").val("");
                    $("#storePassCon").val("");	
                    }
                    organizationImg = datas.data.organizationImg;
                    lng=datas.data.lng;
                    lat=datas.data.lat;
                    _init_baidu_map_by_location(datas.data.lng, datas.data.lat);
                    var pt = new BMap.Point(lng,lat);
                    var marker2 = new BMap.Marker(pt);  // 创建标注
                    map.addOverlay(marker2);
                    $('#target').distpicker({
                        province: pcadata[100000][datas.data.provinceId],
                        city: pcadata[datas.data.provinceId][datas.data.cityId],
                        district: pcadata[datas.data.cityId][datas.data.areaId]
                    });
                } else if (datas.status == "301") {
                    func_artdialog("提示", "加载失败");
                }
            }
        });
    }

    //添加门店
    function saveStore() {
        console.log("=---------province" + $("#s_province").val());
        var url = "<%=path%>/store/ajaxSaveStore";
        var msg = "";
        var judge = true;
        if ($('#name').val() != null && $('#name').val().trim() != "") {
            name = $('#name').val();
        } else {
            msg = "门店名称不能为空！";
            judge = false;
        }
        
        if (judge) {
            if ($('#phone').val() != null && $('#phone').val().trim() != "") {
            	phone = $('#phone').val();
            } else {
                msg = "请填写联系电话！";
                judge = false;
            }
        }
        
        if (judge) {
            if (StringEmpty($("#s_province").val()) && StringEmpty($("#s_city").val()) && StringEmpty($("#s_county").val())) {
                province = $("#s_province option:selected").attr("data-code");
                city = $("#s_city option:selected").attr("data-code");
                area = $("#s_county option:selected").attr("data-code");
            } else {
                msg = "请选择所在地区！";
                judge = false;
            }
        }
        
        if (judge) {
            if ($('#address').val() != null && $('#address').val().trim() != "") {
                address = $('#address').val();
            } else {
                msg = "详细地址不能为空！";
                judge = false;
            }
        }
        
        if (judge) {
            if (lng.length === 0 || lat.length === 0) {
                msg = "请在地图中标注门店位置！";
                judge = false;
            }
        }
        
        //如果确认密码不为空，门店密码为空，提示请填写门店密码
        if (judge) {
            if ($('#storePassCon').val().trim() != "" && $('#storePass').val().trim() == "") {
                msg = "请填写门店密码！";
                judge = false;
            } 
        }
        
        //如果门店密码不为空,确认密码为空，提示请填写确认密码
        if (judge) {
            if ($('#storePass').val().trim() != "" && $('#storePassCon').val().trim() == "") {
                msg = "请填写确认密码！";
                judge = false;
            } 
        }
        if (judge) {
            if ($('#storePass').val().trim() != "" && $('#storePassCon').val().trim() != "" ) {
            //增加密码认证
            if (!checkNumber($('#storePass').val()) || !checkNumber($('#storePassCon').val()) || $('#storePass').val().length != 6 || $('#storePassCon').val().length != 6) {
                    msg = "密码只能为六位数字！";
                    judge = false;
          	 }else{
                  msg = "";
                  judge = true;
          	 }
            }else{
                  msg = "";
                  judge = true;
            }
         }
        //如果门店密码不为空，确认密码不为空，门店密码与确认密码不相同，那么提示  密码输入不一致
        if (judge) {
            if ($('#storePass').val().trim() != "" && $('#storePassCon').val().trim() != "" && $('#storePass').val().trim() != $('#storePassCon').val().trim()) {
                msg = "密码输入不一致！";
                judge = false;
            } 
        }
        
        //如果门店密码不为空，确认密码不为空，门店密码与确认密码相同，那么直接赋值
        if (judge) {
            if ($('#storePass').val().trim() != "" && $('#storePassCon').val().trim() != "" && $('#storePass').val().trim() == $('#storePassCon').val().trim()) {
            	//拿到一个正确的密码
                storePassCon = $('#storePassCon').val();
            } 
        }
        
/*         if (judge) {
            if ($('#phone').val() != null && $('#phone').val().trim() != "" && /^1[34578]\d{9}$/.test($('#phone').val())) {
                phone = $('#phone').val();
            } else if ($('#phone').val() != null && $('#phone').val().trim() != "") {
                msg = "手机号格式不正确！";
                judge = false;
            }
        }
 */
        
       
        if ($("#organizationCode").val() != null && $("#organizationCode").val().trim() != "") {
            organizationCode = $("#organizationCode").val();
        }
        if (judge) {
            $.post(url, {
                "access_token": access_token,
                "tenantId": cid,
                "uuid": uid,
                "name": name,
                "address": address,
                "phone": phone,
                "organizationCode": organizationCode,
                "organizationImg": organizationImg,
                "lng": lng,
                "lat": lat,
                "provinceId": province,
                "cityId": city,
                "areaId": area,
                "pwd":storePassCon 
            }, function (data) {
                if (data.status == "200") {
                    func_artdialog("提示", '添加成功', "");
                    window.location.href = "<%=path%>/view/ui/baseStoreShow";
                } else if (data.status == "301") {
                    func_artdialog("提示", '该门店名称已存在，请勿重复创建', "");
                } else {
                    func_artdialog("提示", data.message);
                }
            });
        } else {
            func_artdialog("提示", msg);
        }
    }

    //编辑门店
    function editStore() {
        var url = "<%=path%>/store/ajaxUpdateStore";
        var msg = "";
        var judge = true;
        if ($('#name').val() != null && $('#name').val().trim() != "") {
            name = $('#name').val();
        } else {
            msg = "门店名称不能为空！";
            judge = false;
        }
        
        if (judge) {
            if ($('#phone').val() != null && $('#phone').val().trim() != "") {
            	phone = $('#phone').val();
            } else {
                msg = "请填写联系电话！";
                judge = false;
            }
        }
        
        if (judge) {
            if (StringEmpty($("#s_province").val()) && StringEmpty($("#s_city").val()) && StringEmpty($("#s_county").val())) {
            	province = $("#s_province option:selected").attr("data-code");
                city = $("#s_city option:selected").attr("data-code");
                area = $("#s_county option:selected").attr("data-code");
            } else {
                msg = "请选择所在地区！";
                judge = false;
            }
        }
        
        if (judge) {			
            if ($('#address').val() != null && $('#address').val().trim() != "") {
                address = $('#address').val();
            } else {
                msg = "详细地址不能为空！";
                judge = false;
            }
        }
        
        if (judge) {
            if (lng.length === 0 || lat.length === 0) {
                msg = "请在地图中标注门店位置！";
                judge = false;
            }
        }
        
        //如果确认密码不为空，门店密码为空，提示请填写门店密码
        if (judge) {
            if ($('#storePassCon').val().trim() != "" && $('#storePass').val().trim() == "") {
                msg = "请填写门店密码！";
                judge = false;
            } 
        }
        
        //如果门店密码不为空,确认密码为空，提示请填写确认密码
        if (judge) {
            if ($('#storePass').val().trim() != "" && $('#storePassCon').val().trim() == "") {
                msg = "请填写确认密码！";
                judge = false;
            } 
        }
        if (judge) {
        if ($('#storePass').val().trim() != "" && $('#storePassCon').val().trim() != "" ) {
        //增加密码认证
        if (!checkNumber($('#storePass').val()) || !checkNumber($('#storePassCon').val()) || $('#storePass').val().length != 6 || $('#storePassCon').val().length != 6) {
        	if("abcdef" != $('#storePass').val() && "abcdef" != $('#storePassCon').val()){
                msg = "密码只能为六位数字！";
                judge = false;
        	}else{
                msg = "";
                judge = true;
        	}
      	 }else{
              msg = "";
              judge = true;
      	 }
        }else{
              msg = "";
              judge = true;
        }
        }
        
        //如果门店密码不为空，确认密码不为空，门店密码与确认密码不相同，那么提示  密码输入不一致
        if (judge) {
            if ($('#storePass').val().trim() != "" && $('#storePassCon').val().trim() != "" && $('#storePass').val().trim() != $('#storePassCon').val().trim()) {
                msg = "密码输入不一致！";
                judge = false;
            } 
        }
        
        //如果门店密码不为空，确认密码不为空，门店密码与确认密码相同，那么直接赋值
        if (judge) {
            if ($('#storePass').val().trim() != "" && $('#storePassCon').val().trim() != "" && $('#storePass').val().trim() == $('#storePassCon').val().trim()) {
            	//拿到一个正确的密码
                storePassCon = $('#storePassCon').val();
            } 
        }
        
        //phone = $('#phone').val();
        
/*         if (judge) {
            if ($('#phone').val() != null && $('#phone').val().trim() != "" && /^1[34578]\d{9}$/.test($('#phone').val())) {
                phone = $('#phone').val();
            } else if ($('#phone').val() != null && $('#phone').val().trim() != "") {
                msg = "手机号格式不正确！";
                judge = false;
            }
        } */
        if ($("#organizationCode").val() != null && $("#organizationCode").val().trim() != "") {
            organizationCode = $("#organizationCode").val();
        }
        
        if (judge) {
            $.post(url, {
                "id": id,
                "access_token": access_token,
                "tenantId": cid,
                "uuid": uid,
                "name": name,
                "address": address,
                "phone": phone,
                "organizationCode": organizationCode,
                "organizationImg": organizationImg,
                "lng": lng,
                "lat": lat,
                "provinceId": province,
                "cityId": city,
                "areaId": area,
                "pwd":storePassCon 
            }, function (data) {
                if (data.status == "200") {
                    window.location.href = "<%=path%>/view/ui/baseStoreShow"
                } else if (data.status == "301") {
                    func_artdialog("提示", "该门店名称已存在！"); 
                }  else {
                    func_artdialog("提示", "编辑失败，请重试！");
                }
            });
        } else {
            func_artdialog("提示", msg);
        }
    }
	 
	 
	 
	//验证字符串是否是数字
	function checkNumber(theObj) {
	  var reg = /^[0-9]+.?[0-9]*$/;
	  if (reg.test(theObj)) {
	    return true;
	  }
	  return false;
	}
	

    function save_or_edit() {
        if (flag == 1) {
            saveStore();
        } else if (flag == 2) {
            editStore();
        }
    }

    //判断字符串是否为空
    function StringEmpty(content) {
        if (content == undefined || content == "" || content == null) {
            return false;
        } else {
            return true;
        }
    }

    function func_artdialog(name, value) {
        var d = dialog({
            title: name,
            fixed: true,
            width: 400,
            height: 100,
            content: "<center style='font-size:20px; line-height:100px;'>" + value + "</center>",
            okValue: "确定",
            ok: function () {
                return true;
            }
        });
        d.show();
    }
    function fileUpload(){
        document.getElementById("inputfile").click();
    }
	$("#inputfile").change(function(){
		var filePath=$("input[name='filepath']").val();
		var extStart = filePath.lastIndexOf(".");
        var ext = filePath.substring(extStart, filePath.length).toUpperCase();
        if (ext == ".BMP" || ext == ".PNG" || ext == ".GIF" || ext == ".JPG" || ext == ".JPEG") {      	
        	fileloadon();
        }else{
        	func_artdialog("提示","请选择图片格式文件！");
        }
	});
    function fileloadon() {
        var flag = false;
        $("#share_image").submit(function () {
            if (flag) {
                return;
            }
            flag = true;
            $("#share_image").ajaxSubmit({
                type: "post",
                url: "${pageContext.request.contextPath}/api/uploadfile/file",
                data: {"typename": "filepath"},
                success: function (data1) {
                    func_artdialog("提示", data1.message);
                    organizationImg = data1.realSavePath;
                    $("#fileName").html("已选文件：" + data1.saveFilename);
                },
                error: function (msg) {
                    func_artdialog("提示", msg);
                }
            });
            return false;
        });
        $("#share_image").submit();
    }

    $("#commit").click(function () {
        save_or_edit();
    });
    $("#close").click(function () {
        window.location.href = "<%=path%>/view/ui/baseStoreShow";
    });

</script>
<script type="text/javascript">
    // 百度地图API功能
    var map = new BMap.Map("allmap");
    map.enableScrollWheelZoom();   //启用滚轮放大缩小，默认禁用
    map.enableContinuousZoom();

    function _init_map(flag) {
        if (flag == null) {
            map.centerAndZoom("北京", 12);
            var geolocation = new BMap.Geolocation();
            geolocation.getCurrentPosition(function (r) {
                if (this.getStatus() === BMAP_STATUS_SUCCESS) {
                    var mk = new BMap.Marker(r.point);
                    map.addOverlay(mk);
                    map.panTo(r.point);
                }
                else {
                    alert('failed' + this.getStatus());
                }
            }, {enableHighAccuracy: true});
        }
        map.addEventListener("click", function (e) {
            map.clearOverlays();
            var pt = new BMap.Point(e.point.lng, e.point.lat);
//                var myIcon = new BMap.Icon("http://lbsyun.baidu.com/jsdemo/img/fox.gif", new BMap.Size(300, 157));
//                var marker2 = new BMap.Marker(pt, {icon: myIcon});  // 创建标注
            var marker2 = new BMap.Marker(pt);  // 创建标注
            map.addOverlay(marker2);
            lng = e.point.lng;
            lat = e.point.lat;
        });
    }

    function _init_baidu_map_by_city(city) {
        map.clearOverlays();
        if (city === null || city.length < 1) {
            city = "北京"
        }
        map.centerAndZoom(city, 12);
    }

    function _init_baidu_map_by_location(lng, lat) {
        map.clearOverlays();
        var point = new BMap.Point(lng, lat);
        map.centerAndZoom(point, 12);
    }


    function selectAddress() {
        //console.log(1);
        var address = "";
        var province = $("#s_province").val();
        var city = $("#s_city").val();
        var county = $("#s_county").val();

        if (province !== "省份") {
            address += province;
        }
        if (city !== "地级市") {
            address += city;
        }
        if (county !== "市、县级市") {
            address += county;
        }
        _init_baidu_map_by_city(address);
    }

</script>
<script type="text/javascript" src="http://tajs.qq.com/stats?sId=9051096" charset="UTF-8"></script>
</body>
</html>
